import ArrayUtil from '@/ArrayUtil';
import BaseUtil from '@/BaseUtil';
import BuffUtil from '@/BuffUtil';
import RandomUtil from '@/RandomUtil';
import SelectUtil from '@/SelectUtil';
import UnitCommon from 'code/Core/Common/UnitCommon';
import TalentBase from 'code/Scripts/Abilities/base/TalentBase';
const desc = `你被攻击时有${ColorSafe}17%|r攻击对周围${ColorSafe}275|r范围内的敌人造成${ColorSafe}攻击x75%|r的物理伤害。触发间隔${ColorSafe}0.8|r秒|n觉醒效果：触发间隔降低至${ColorSafe}0.4|r秒，并在触发时提升${ColorSafe}100%|r的攻击和生命回复效果，持续${ColorSafe}3|rs。`;
const art = 'ReplaceableTextures\\CommandButtons\\BTNStaffOfSanctuary.blp';
const name = '反击螺旋';
const config: TalentConfigModel = {
    id: 'talent_t_reflectrotate',
    name,
    desc,
    art,
    buffType: {
        id: 'buff_t_reflectrotate',
        art,
        name,
        ubertip: desc,
    },
    buffTypePlus: {
        id: 'buff_t_reflectrotate_plus',
        art,
        name: '反击螺旋[觉醒]',
        ubertip: `你被攻击时有${ColorSafe}17%|r攻击对周围${ColorSafe}275|r范围内的敌人造成${ColorSafe}攻击x75%|r的物理伤害。触发间隔${ColorSafe}0.4|r秒|n，并在触发时提升${ColorSafe}100%|r的攻击和生命回复效果，持续${ColorSafe}3|rs`,
    },
};

const effectBuffType: AppBuffType = {
    id: 'buff_t_reflectrotate_effect',
    art,
    name: '反击螺旋',
    ubertip: '提升攻击与生命回复效果',
    dur: 3,
    stackCountMax: -1,
    attribute: {
        attack_p: 1,
        生命恢复增强: 1,
    },
};

export default class TalentReflectRotate extends TalentBase {
    static instance: TalentReflectRotate;
    constructor(p?: TalentConfigModel) {
        super(p);
        TalentReflectRotate.instance = this;
        this.setTalentConfig(config);
        this.init();
        EventManager.onTakeAttack(this.attackEffect.bind(this));
    }
    private _coolPool: unit[] = [];
    attackEffect(d: DamageEventData) {
        if (TalentReflectRotate.instance._coolPool.includes(d.target)) return;
        let { unEffect, plus } = TalentReflectRotate.instance.talentUnEffect(d.target);
        if (unEffect) return;
        let r = RandomUtil.randomPercent();
        if (r > 0.17) return;
        TalentReflectRotate.instance._coolPool.push(d.target);
        let delay = plus ? 0.4 : 0.8;
        BaseUtil.runLater(delay, () => {
            ArrayUtil.removeElement(TalentReflectRotate.instance._coolPool, d.target);
        });
        let es = SelectUtil.getEnemyUnitsInRange(d.target, 275);
        let dmg = UnitCommon.取攻击(d.target) * 0.75;
        for (let e of es) {
            DamageSystemInstance.applyPhysicalDamage(e, d.target, dmg);
        }
        if (plus) {
            BuffUtil.addBuff(d.target, effectBuffType.id, d.target);
        }
        SetUnitAnimation(d.target, 'Attack Walk Stand Spin');
        UnitCommon.普攻冷却(d.target);
    }
    init() {
        BuffUtil.registerBuffType(effectBuffType);
    }
}
